package org.tgcloud.group.core.mapUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tgcloud.group.core.HttpClientUtil;
import org.tgcloud.group.core.RedisUtil;
import org.tgcloud.group.core.mapUtils.data.GDResultData;
import org.tgcloud.group.core.mapUtils.data.LocationData;
import org.tgcloud.group.core.mapUtils.data.TXResultData;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhy
 * @Created: 2025-01-24-下午4:07
 * @Description: 腾讯地图工具
 */
@Component
public class TXMapUtils {

    private static final String key = "RKSBZ-T4NLQ-IO65Y-BZSWS-FHFJE-WRBTL";

//    private static final String gaoDeKey = "d767b66b4ad6481e59cb12528dffeb62";
    private static final String gaoDeKey = "e864d7e24c10832a5708c9218f822361";

    @Autowired
    private HttpClientUtil clientUtil;

    public TXResultData.ResultData.Location getTXGeocoderToLocation(String add){
        String address = add.replace(" ", "")
                .replace("　", "")
                .replace("/", "")
                .replace("|", "")
                .replace("\\", "")
                .replace("\n", "")
                .replace(" ", "")
                .replace("\\u00A0", "")
                .replace("*不可选点", "")
                .replace("#", "号")
                .replace("-", ""); // 清除传入的参数有空格的情况
        TXResultData.ResultData.Location location = new TXResultData.ResultData.Location();
        if (RedisUtil.existKey(address)){
            return RedisUtil.getCacheObject(address);
        }
        String msg = clientUtil.doGet("https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=" + key, null);
        TXResultData info = JSON.parseObject(msg, new TypeReference<TXResultData>() {
        });
        System.out.println("接口返回=====" + JSON.toJSONString(info) + "=============" + add);
        if (info.getStatus().equals(0)){
            location = info.getResult().getLocation();
            RedisUtil.setCacheObject(address, location, 30L, TimeUnit.DAYS);
        }
        return location;
    }



    public LocationData getGDGeocoderToLocation(String add){
        String address = add.replace(" ", "")
                .replace("　", "")
                .replace("/", "")
                .replace("|", "")
                .replace("\\", "")
                .replace("\n", "")
                .replace(" ", "")
                .replace("\\u00A0", "")
                .replace("*不可选点", "")
                .replace("#", "号")
                .replace("-", ""); // 清除传入的参数有空格的情况
        // 根据address获取缓存中的地址信息。如果没有，那么获取后直接存入缓存。如果有，那么直接使用
        if (RedisUtil.existKey(address)){
            return RedisUtil.getCacheObject(address);
        }
        LocationData location = new LocationData();
        String msg = clientUtil.doGet("https://restapi.amap.com/v3/geocode/geo?address=" + address + "&key=" + gaoDeKey, null);
        System.out.println("接口返回=====" + msg + "=============" + address);
        GDResultData info = JSONObject.parseObject(msg, GDResultData.class);
        if (info.getStatus().equals("1")){
            String locationMsg = info.getGeocodes().get(0).getLocation();
            String[] split = locationMsg.split(",");
            String lng = split[0];
            String lat = split[1];
            location.setLng(lng);
            location.setLat(lat);
            RedisUtil.setCacheObject(address, location, 30L, TimeUnit.DAYS);
        }
        return location;
    }

}
